/******************************************************************************
 * The contents of this file are subject to the   JDI License  Version 1.0
 * ("License"); You may not use this file except in compliance with the License
 * Software distributed under the License is distributed on an  "AS IS"  basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 * The Initial Developer of the Original Code is 陈慧然. Portions created by 
 * 陈慧然 are Copyright 陈慧然.
 * All Rights Reserved.
 *
 *              Contact john_holen@163.com for licensing alternatives.
 *
 *****************************************************************************/


package org.peony.client.web;

import org.peony.exceptions.RenderException;
import org.peony.standard.ui.SIContainer;
import org.peony.standard.ui.SILayOut;
import org.peony.tools.StringUtil;

/**
 *  
 *
 *  @author     陈慧然
 *  @version    Id: WebPage.java, v 0.000 2008-4-19 下午10:12:48 陈慧然 Exp
 */
public class WebPage implements SIContainer{

	private static final String DOCTYPE = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">";
	private static final String empty =StringUtil.empty;
	private String title = empty;
	private String metaCharSet = empty;		//网页使用的字符集，默认为“UTF-8”
	private String metaFileType = empty;	//文件格式
	private String metaKeyWords = empty;	//网页关键字
	private String metaDescription = empty;	//网页描述
	private String metaAuthor = empty;		//网页作者
	private boolean isRefresh = false;		//是否可以重定向
	private int waitSecondsForRefresh = 0;	//重定向等待时间
	private String refreshUrl = empty;		//重定向URL
	private SILayOut layout; 				//页面布局管理器
	private String id = empty;
	private static final String CSS = "ROOT";
	public void clear() {
		layout = null;
	}
	/**
	 * 获得该网页的布局管理器
	 */
	public SILayOut getLayOut() {
		return layout;
	}
	/**
	 * 设置该网页的布局管理器
	 */
	public void setLayOut(SILayOut layout) {
		this.layout = layout;
	}

	
	public String toStandardString() throws RenderException{
		if(StringUtil.isEmpty(id)){
			throw new RenderException("WebPage(A Container) Can NOT has no id!");
		}
		StringBuffer sb = new StringBuffer();
		sb.append(DOCTYPE)
			.append(toStringIncludeJs())
			.append("<html>")
				.append(toStringHead())
				.append("<body><div class=\"").append(CSS).append("\">")				
				.append(getLayOut()==null?empty:getLayOut().toStandardString())
			.append("</div></body></html>");
		return sb.toString();
	}
	
	/**
	 * 设置网页标题
	 * @param title
	 */
	public void setTitle(String title){
		this.title = title;
	}
	
	/**
	 * 得到网页字符集
	 * @return
	 */
	public String getMetaCharSet() {
		if(StringUtil.isEmpty(this.metaCharSet))
			return "UTF-8";
		return metaCharSet;
	}
	/**
	 * 设置网页字符集
	 * @param metaCharSet
	 */
	public void setMetaCharSet(String metaCharSet) {
		this.metaCharSet = metaCharSet;
	}
	/**
	 * 得到网页格式
	 * @return
	 */
	public String getMetaFileType() {
		if(StringUtil.isEmpty(this.metaFileType))
			return "text/html";
		return metaFileType;
	}
	/**
	 * 设置网页格式
	 * @param metaFileType
	 */
	public void setMetaFileType(String metaFileType) {
		this.metaFileType = metaFileType;
	}
	/**
	 * 得到网页关键字
	 * @return
	 */
	public String getMetaKeyWords() {
		return metaKeyWords;
	}
	/**
	 * 设置网页关键字
	 * @param metaKeyWords
	 */
	public void setMetaKeyWords(String metaKeyWords) {
		this.metaKeyWords = metaKeyWords;
	}
	/**
	 * 得到网页描述
	 * @return
	 */
	public String getMetaDescription() {
		return metaDescription;
	}
	/**
	 * 设置网页描述
	 * @param metaDescription
	 */
	public void setMetaDescription(String metaDescription) {
		this.metaDescription = metaDescription;
	}
	/**
	 * 得到网页作者
	 * @return
	 */
	public String getMetaAuthor() {
		if(StringUtil.isEmpty(metaAuthor))
			return "chenhuiran, Leonard Chen For English";
		return metaAuthor;
	}
	/**
	 * 设置网页作者
	 * @param metaAuthor
	 */
	public void setMetaAuthor(String metaAuthor) {
		this.metaAuthor = metaAuthor;
	}
	/**
	 * 该网页是否能被重定向
	 * @return
	 */
	public boolean isRefresh() {
		return isRefresh;
	}
	/**
	 * 得到网页重定向URL
	 * @return
	 */
	public String getRefreshUrl() {
		return refreshUrl;
	}
	/**
	 * 设置网页重定向URL
	 * @param refreshUrl
	 */
	public void setRefreshUrl(int waitSecondsForRefresh,String refreshUrl) {
		if(StringUtil.isEmpty(refreshUrl)){
			this.isRefresh = false;
			this.refreshUrl = empty;
		}else{
			if(waitSecondsForRefresh < 0)
				this.waitSecondsForRefresh = 0;
			this.isRefresh = true;
			this.waitSecondsForRefresh = waitSecondsForRefresh;
			this.refreshUrl = refreshUrl;
		}
	}
	
	/**
	 * 得到网页标题
	 * @return
	 */
	public String getTitle() {
		if(StringUtil.isEmpty(this.title))
			return "Peony - A Project For My Wife.";
		return title;
	}

	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	
	public String getUIClass() {
		return CSS;
	}
	public void setUIClass(String css) {
		;
	}
	/**
	 * 输出标准[head][/head]区块内容
	 * @return
	 */
	private String toStringHead(){
		StringBuffer sb = new StringBuffer();
		sb.append("<head>");
		sb.append("<meta http-equiv=\"Content-Type\" content=\"")
			.append(this.getMetaFileType()).append("; charset=")
			.append(this.getMetaCharSet()).append("\" />");
		if(isRefresh){
			sb.append("<meta http-equiv=\"Refresh\" content=\"")
				.append(waitSecondsForRefresh).append("; url=").append(this.getRefreshUrl()).append("\" />");
		}
		sb.append("<meta name=\"author\" content=\"").append(this.getMetaAuthor()).append("\" />");
		if(StringUtil.isNotEmpty(metaDescription)){
			sb.append("<meta name=\"description\" content=\"").append(this.getMetaDescription()).append("\" />");
		}
		if(StringUtil.isNotEmpty(metaKeyWords)){
			sb.append("<meta name=\"keywords\" content=\"").append(this.getMetaKeyWords()).append("\" />");
		}
		sb.append(toStringIncludeCss());
		sb.append("<title>").append(this.getTitle()).append("</title>");
		sb.append("</head>");
		return sb.toString();
	}
	
	private String toStringIncludeCss(){
		return empty;
	}
	
	private String toStringIncludeJs(){
		return empty;
	}
	
}
